<?php
# $dbEngine, $dbHostname, $dbPassword, $dbPortNumber, $dbUsername, $dbPassword, $dbName, $dbPrefix
require_once 'config/config.php';
session_start();

if ($dbEngine = 'sqlite') {
  if ($db = new SQLiteDatabase('rpgc.sqlite')) {
    $q = @$db->query('SELECT username FROM ' . $dbPrefix . 'users WHERE uid = 1');
    if ($q === false) {
      $db->queryExec('CREATE TABLE ' . $dbPrefix . 'users (uid int, username char(80), password char(80), PRIMARY KEY (uid))'); 
      $db->queryExec('INSERT INTO ' . $dbPrefix . 'users VALUES (1,"admin","admin")');
      print "Default database created, administrative user is 'admin', password is 'admin'<BR>\n";
    }
  } else {
    die("Arrg: $err");
  }
}
#$q = @$db->query('SELECT * FROM ' . $dbPrefix . 'users');
#print "<TABLE>\n";
#print "<TR><TD align=center>UID</TD><TD align=center>Username</TD><TD align=center>Password</TD></TR>\n";
#while ($q->valid()) {
#    $row = $q->current();
#    print "<TR><TD align=center>$row[0]</TD><TD align=left>$row[1]</TD><TD align=left>$row[2]</TD></TR>\n";
#    $q->next();
#}
#print "</TABLE>\n";
#unset ($db);
if (isset($_POST['txtUserId']) && isset($_POST['txtPassword'])) {
   $username = $_POST['txtUserId'];
   $password = $_POST['txtPassword'];
   $q = @$db->query('SELECT uid FROM ' . $dbPrefix . 'users WHERE username = "' . $userId . '" AND password = "' . $userPassword . '"');
   $rows = sqlite_num_rows($q);
   if ($rows == 1) {
      $_SESSION['user_logged_in'] = true;
      header('Location: index.php');
      exit;
   } else {
      print "$rows number of rows.<BR>\n";
      print "Wrong username or password<BR>\n";
   }
}
?>
<html>
  <head><title>Login?</title></head>
  <body>
    <form method="post" name="frmLogin" id="frmLogin">
      <table width="400" border="1" align="center" cellpadding="2" cellspacing="2">
        <tr> 
          <td width="150">User Id</td> 
          <td><input name="txtUserId" type="text" id="txtUserId"></td>
        </tr>
        <tr>
          <td width="150">Password</td>
          <td><input name="txtPassword" type="password" id="txtPassword"></td>
         </tr>
        <tr>
          <td width="150"> </td>
          <td><input type="submit" name="btnLogin" value="Login"></td>
        </tr>
      </table>
    </form>
  </body>
</html>
